const db = require('../../db');
const Path = require('path');
var ejsExcel = require("ejsexcel");
var fs = require("fs");


const exlBuf = fs.readFileSync(Path.resolve(__dirname, '../../public/工作周报模板.xlsx'));

let exportWeekReport = {
    config: {
        description: '周报导出',
        cors: {
            origin: ['*'],
            additionalHeaders: ['cache-control', 'x-requested-with']
        }
    },
    method: 'post',
    path: '/report/export',
    handler: async (request, h) => {

        let request_body = request.payload

        let stateCode;
        let stateMessage;

        let year = request_body.year
        let week = request_body.week
        let reportData = request_body.content

        let result = {
            data: undefined,
            stateCode: '',
            stateMessage: ''
        }
        let promise = function () {
            return new Promise((resolve, reject) => {
                console.log('reportData', reportData);
                ejsExcel.renderExcel(exlBuf, reportData).then(function (exlBuf2) {
                    let excelFilePath = Path.resolve(__dirname, `../../public/工作周报${year}年第${week}周.xlsx`);
                    fs.writeFileSync(excelFilePath, exlBuf2);
                    resolve(`工作周报${year}年第${week}周.xlsx`);
                    // 下载
                }).catch(function (err) {
                    console.error(err);
                    reject(err)
                });
            })
        }
        return await promise()
    }
}

module.exports = exportWeekReport